package base.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CreateNewClassReflect {

    public static String className = "base.demo.Person";

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
        createNewClassWithParams();
    }

    //早期：new时候，先根据被new的类的名称找寻该类的字节码文件，并加载进内存，
    //并创建该字节码文件对象，并接着创建该字节文件的对应的Person对象。
    //Person p = new Person();
    public static void createNewClass() throws ClassNotFoundException, IllegalAccessException, InstantiationException {

        //找寻该文件类文件，并加载进内存，并产生Class对象。
        Class clazz = Class.forName(className);

        Object obj = clazz.newInstance();//调用Person的空参构造函数
    }


    public static void createNewClassWithParams() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Class.forName(className);

        Constructor constructor=clazz.getConstructor(int.class,String.class);

        Object obj = constructor.newInstance(59,"tom");
    }


}












